# -*- coding: utf-8 -*-
import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template



from xmw import tianya
from xmw.tianya import error_tianya

import os


class MainPage(webapp.RequestHandler):
  def get(self):
    if  self.request.GET.has_key('mail_box') and self.request.GET.has_key('url'):
        #self.response.out.write(template.render(path, template_values))
        mail    =self.request.GET['mail_box']
        url =self.request.GET['url']
        req =tianya.SubReqest(mail_box=mail,url=url,is_binded=False,is_threaded=False)
        #req =tianya.SubReqest(mail,url)
        req.put()
        #self.response.out.write("mail=%s,url=%s"%(mail,url))
        #req.put()
        e = error_tianya.OK(str(self.request))        
        
        
    else:
        e = error_tianya.ARGUMENT_FAULT("/subscribe?mail_box=user@gmail.com;url=XXXX, where now it is %s"%str(self.request))
    e.log()
    template_values = {
                        "error":e
                    }     
    path = os.path.join(os.path.dirname(__file__), 'template/error.html')
    self.response.out.write(template.render(path, template_values))
        

def main():
    import global_setting
    
    application = webapp.WSGIApplication(
                                       [('/subscribe', MainPage)],
                                       debug=global_setting.is_debug())                                       
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
    main()
